package com.example.springbootyczy.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.springbootyczy.DTO.BooksSearchDTO;
import com.example.springbootyczy.entity.*;
import com.example.springbootyczy.service.IBooksService;
import com.example.springbootyczy.service.IStudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping(value = "/books")
public class BooksConroller {
    @Autowired
    IBooksService booksService;

    //分页查询:按条件查询
    @PostMapping("/page")
    public ResultResponse<List<Books>> page(@RequestBody BooksSearchDTO param){
        //创建翻页对象：Page: size(页大小), current(当前页), total(总条数)
        Page<Books> page = new Page<>();
        page.setCurrent(param.getCurrent());
        page.setSize(param.getSize());
        //调用mybatisplus进行分页
        Page<Books> pageStduents = booksService.page(page, null);
        //返回
        return ResultResponse.success("1000", "成功", pageStduents);
    }

    //修改
    @PostMapping("/edit")
    public ResultResponse<String> edit(@RequestBody StudentEdit param){
        //对象赋值
        Books books = BeanUtil.copyProperties(param, Books.class);
        boolean saveFalg = booksService.updateById(books);
        //返回
        return ResultResponse.success("1000", "成功", saveFalg+"");
    }
}
